namespace Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_RegRw
{
    using System;
    using System.Text;

    using Microsoft.Phone.InteropServices;

    using Yaaf.WP7.NativeAccess.Helper;
    using Yaaf.WP7.NativeAccess.Registry;

    internal class HTCRegistry : IDisposable
    {
        // Fields
        private const string m_ComGuid = "020CF84D-5946-4C9B-A066-8C9EC348DEDD";
        private IRegRwInterface m_RegRwObject;
        public const int MAX_PATH = 260;
        public const int MAX_STRLEN = 0x100;

        // Methods
        public HTCRegistry()
        {
            Devices.Initialize();
            InteropHelper.RegisterDll("COM_RegRw.dll", m_ComGuid);
            this.m_RegRwObject = (IRegRwInterface)new CRegRwClass();
            this.Init();
        }

        private bool validateString(string A_0)
        {
            return ((MAX_STRLEN >= A_0.Length) && (0 <= A_0.Length));
        }

        private bool IsError(uint A_0)
        {
            return (A_0 != 0);
        }

        public uint Deinit()
        {
            uint num = 0;
            this.m_RegRwObject.Deinit(out num);
            return num;
        }

        public bool GetVersionInfo(string szSearchKey, out StringBuilder szRequestVersion)
        {
            uint num = 0;
            szRequestVersion = new StringBuilder(MAX_PATH);
            this.m_RegRwObject.GetVersionInfo(szSearchKey, szRequestVersion, out num);
            return this.IsError(num);
        }

        public uint Init()
        {
            uint num = 0;
            this.m_RegRwObject.Init(out num);
            return num;
        }

        public bool IsAPExist(string szProductID)
        {
            uint num = 0;
            this.m_RegRwObject.IsAPExist(szProductID, out num);
            return this.IsError(num);
        }

        public bool IsHTCDevice()
        {
            uint isHTCDevice = 0;
            this.m_RegRwObject.IsHTCDevice(out isHTCDevice);
            return (isHTCDevice == 0xb9d548dc);
        }
       
        public bool LaunchAP(string szProductID)
        {
            uint num = 0;
            this.m_RegRwObject.LaunchAP(szProductID, out num);
            return this.IsError(num);
        }

        public bool RegistryGetDWORD(RootKey eKey, string strSubKey, string strValueName, out uint uData)
        {
            uint num = 0;
            uData = 0;
            if (this.validateString(strSubKey) && this.validateString(strValueName))
            {
                this.m_RegRwObject.RegistryGetDWORD((uint)eKey, strSubKey, strValueName, out uData, out num);
            }
            return this.IsError(num);
        }

        public bool RegistryGetString(RootKey eKey, string strSubKey, string strValueName, out StringBuilder strData)
        {
            uint num = 0;
            strData = new StringBuilder(MAX_PATH);
            if (this.validateString(strSubKey) && this.validateString(strValueName))
            {
                this.m_RegRwObject.RegistryGetString((uint)eKey, strSubKey, strValueName, strData, out num);
            }
            return this.IsError(num);
        }

        public bool RegistrySetDWORD(RootKey eKey, string strSubKey, string strValueName, uint uData)
        {
            uint num = 0;
            if (this.validateString(strSubKey) && this.validateString(strValueName))
            {
                this.m_RegRwObject.RegistrySetDWORD((uint)eKey, strSubKey, strValueName, uData, out num);
            }
            return this.IsError(num);
        }

        public bool RegistrySetString(RootKey eKey, string strSubKey, string strValueName, string pszData)
        {
            uint num = 0;
            if ((this.validateString(strSubKey) && this.validateString(strValueName)) && this.validateString(pszData))
            {
                this.m_RegRwObject.RegistrySetString((uint)eKey, strSubKey, strValueName, pszData, out num);
            }
            return this.IsError(num);
        }
        

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~HTCRegistry()
        {
            this.Dispose(false);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.Deinit();
            }
        }

    }


    //public sealed class HTCRegistry : IDisposable
    //{

    //    // Fields
    //    private const string m_ComGuid = "020CF84D-5946-4C9B-A066-8C9EC348DEDD";
    //    private IRegRwInterface m_RegRwObject;
    //    public const int MAX_PATH = 260;
    //    public const int MAX_STRLEN = 0x100;

    //    // Methods
    //    public HTCRegistry()
    //    {
    //        Devices.Initialize();
    //        if (ComBridge.RegisterComDll("COM_RegRw.dll", new Guid(m_ComGuid)) != 0)
    //        {
    //            throw new Exception("Register COM Dll Fail.");
    //        }
    //        this.m_RegRwObject = (IRegRwInterface)new CRegRwClass();
    //        this.Init();
    //    }



    //    public uint Deinit()
    //    {
    //        uint uResult = 0;
    //        this.m_RegRwObject.Deinit(out uResult);
    //        return uResult;
    //    }

    //    public bool GetVersionInfo(string szSearchKey, out StringBuilder szRequestVersion)
    //    {
    //        uint uRet = 0;
    //        szRequestVersion = new StringBuilder(MAX_PATH);
    //        this.m_RegRwObject.GetVersionInfo(szSearchKey, szRequestVersion, out uRet);
    //        return this.Return(uRet);
    //    }

    //    public uint Init()
    //    {
    //        uint uResult = 0;
    //        this.m_RegRwObject.Init(out uResult);
    //        return uResult;
    //    }

    //    public bool IsHTCDevice()
    //    {
    //        uint isHTCDevice = 0;
    //        this.m_RegRwObject.IsHTCDevice(out isHTCDevice);
    //        return (isHTCDevice == 0xb9d548dc);
    //    }

    //    public bool RegistryGetDWORD(RootKey eKey, string strSubKey, string strValueName, out uint uData)
    //    {
    //        uint uResult = 0;
    //        uData = 0;
    //        if (this.StringValid(strSubKey) && this.StringValid(strValueName))
    //        {
    //            this.m_RegRwObject.RegistryGetDword((uint)eKey, strSubKey, strValueName, out uData, out uResult);
    //        }
    //        return this.Return(uResult);
    //    }

    //    public bool RegistryGetString(RootKey eKey, string strSubKey, string strValueName, out StringBuilder strData)
    //    {
    //        uint uResult = 0;
    //        strData = new StringBuilder(MAX_PATH);
    //        if (this.StringValid(strSubKey) && this.StringValid(strValueName))
    //        {
    //            this.m_RegRwObject.RegistryGetString((uint)eKey, strSubKey, strValueName, strData, out uResult);
    //        }
    //        return this.Return(uResult);
    //    }

    //    public bool RegistrySetDWORD(RootKey eKey, string strSubKey, string strValueName, uint uData)
    //    {
    //        uint uResult = 0;
    //        if (this.StringValid(strSubKey) && this.StringValid(strValueName))
    //        {
    //            this.m_RegRwObject.RegistrySetDword((uint)eKey, strSubKey, strValueName, uData, out uResult);
    //        }
    //        return this.Return(uResult);
    //    }

    //    public bool RegistrySetString(RootKey eKey, string strSubKey, string strValueName, string pszData)
    //    {
    //        uint uResult = 0;
    //        if ((this.StringValid(strSubKey) && this.StringValid(strValueName)) && this.StringValid(pszData))
    //        {
    //            this.m_RegRwObject.RegistrySetString((uint)eKey, strSubKey, strValueName, pszData, out uResult);
    //        }
    //        return this.Return(uResult);
    //    }

    //    private bool Return(uint uRet)
    //    {
    //        return (uRet != 0);
    //    }

    //    private bool StringValid(string strCheck)
    //    {
    //        return ((MAX_STRLEN >= strCheck.Length) && (0 < strCheck.Length));
    //    }

    //    // Nested Types
    //    public enum ACTION_TYPE
    //    {
    //        AT_NOACTION,
    //        AT_NOTIFY,
    //        AT_CLOSEAP
    //    }

    //    public enum RootKey
    //    {
    //        E_HKEY_CLASSES_ROOT,
    //        E_HKEY_CURRENT_USER,
    //        E_HKEY_LOCAL_MACHINE,
    //        E_HKEY_USERS
    //    }

    //    public void Dispose()
    //    {
    //        this.Dispose(true);
    //        GC.SuppressFinalize(this);
    //    }

    //    ~HTCRegistry()
    //    {
    //        this.Dispose(false);
    //    }

    //    private void Dispose(bool disposing)
    //    {
    //        this.Deinit();

    //    }
    //}


}